package org.taskexecution.taskexecution.controller.scheduledTask;

import com.cephr.accountcenter.application.scheduledTask.TaskDefinitionService;
import com.cephr.accountcenter.application.scheduledTask.TaskExecutionLogService;
import com.cephr.accountcenter.application.scheduledTask.TaskScheduleService;
import com.cephr.accountcenter.infrastructure.common.api.CommonPage;
import com.cephr.accountcenter.infrastructure.common.api.CommonResult;
import com.cephr.accountcenter.infrastructure.presistent.po.scheduledTask.TaskExecutionLog;
import com.cephr.accountcenter.task.TaskScanningService;
import com.cephr.accountcenter.userinterface.request.taskSchedule.TaskScheduleDTO;
import com.cephr.accountcenter.userinterface.request.taskSchedule.TaskScheduleQueryDTO;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Lydond
 * @description: 定时任务控制器
 * @create_date: 2025/10/20-16:33
 */
@Slf4j
@RestController
@RequestMapping("/task")
@Api(tags = "定时任务控制器")
public class ScheduledTaskController {
    @Resource
    TaskScheduleService taskScheduleService;

    @Resource
    TaskScanningService taskScanningService;

    @Resource
    TaskDefinitionService taskDefinitionService;

    @Resource
    TaskExecutionLogService taskExecutionLogService;


    @ApiOperation(value = "创建一个定时任务", notes = "创建一个定时任务")
    @PostMapping(value = "/createTask")
    public CommonResult<?> createTask(@RequestBody TaskScheduleDTO taskScheduleDTO,@CookieValue(value = "ticketId") String ticketId ) {
        log.info("ticeketId" +taskScheduleDTO.getTicketId());
//        taskScheduleDTO.setTicketId(ticketId);
        Long task = taskScheduleService.createTask(taskScheduleDTO.getTaskKey(), taskScheduleDTO.getTaskParams(), taskScheduleDTO.getScheduledExecTime(), taskScheduleDTO.getBusinessId(), taskScheduleDTO.getTicketId());
        return CommonResult.success(task);
    }

    @ApiOperation(value = "手动执行任务", notes = "手动执行任务")
    @PostMapping(value = "/manualScanAndExecuteTasks")
    public CommonResult<?> manualScanAndExecuteTasks() {
        taskScanningService.scanAndExecuteTasks();
        return CommonResult.success();
    }

    @ApiOperation(value = "查询任务类型", notes = "查询任务类型")
    @GetMapping(value = "/getTaskTypes")
    public CommonResult<?> getTaskTypes() {
        return CommonResult.success(taskDefinitionService.gettaskDefinitionList());
    }

    //查询所有任务
    @ApiOperation(value = "查询所有任务", notes = "查询所有任务")
    @PostMapping(value = "/getAllTasks")
    public CommonResult<?> getAllTasks(@RequestBody TaskScheduleQueryDTO taskScheduleQueryDTO) {
        PageHelper.startPage(taskScheduleQueryDTO.getPageNum(), taskScheduleQueryDTO.getPageSize());
        return CommonResult.success(CommonPage.restPage(taskScheduleService.queryTasks(taskScheduleQueryDTO)));
    }

    //根据任务id查询任务详情
    @ApiOperation(value = "根据任务id查询所有相关的日志任务", notes = "根据任务id查询所有相关的日志任务")
    @GetMapping(value = "/getTaskById/{scheduleId}")
    public CommonResult<?> getTaskById(@PathVariable Long scheduleId) {
        List<TaskExecutionLog> taskScheduleLogs = taskExecutionLogService.getBySchdulesId(scheduleId);
        return CommonResult.success(taskScheduleLogs);
    }
}